
package security.business;

import Entity.User;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import login.beans.ConectionBean;
import security.DAO.UserDAO;

/**
 *
 * @author JuanFelipe
 */
public class UserBusiness {
    Connection connection;

    public UserBusiness( Connection currentConnection ) {
        connection = currentConnection;
    }
    
    public ArrayList<User> getUsers() throws SQLException{
        String sql = "Select  * FROM dba_users";
        ArrayList<User> users = new ArrayList<>();
        try {
            ResultSet rs = new UserDAO( connection ).executeStatement( sql );
            while( rs.next() ){
                ArrayList<String> sl = new ArrayList<String>();

                for(int i = 1 ; i <= rs.getMetaData().getColumnCount() ; i++){
                    if( rs.getString(i)==null )
                        sl.add( "(null)" );
                    else
                        sl.add( rs.getString(i) );
                }
                User aux = new User( sl.get(18), sl.get(17), sl.get(16),
                            sl.get(15), sl.get(14), sl.get(13), sl.get(12),
                            sl.get(11), sl.get(10), sl.get(9), sl.get(8),
                            sl.get(7), sl.get(6), sl.get(5), sl.get(4),
                            sl.get(3), sl.get(2), sl.get(1), sl.get(0) );
                users.add( aux );
            }
            
        } catch (SQLException ex) {
            
            
            System.out.println("Error: "+ex);
            throw ex;
            //Logger.getLogger(UserBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return users;
    }
    
    public ArrayList<String> getTableSpaceName()throws SQLException{
        String sql = "select tablespace_name from dba_tablespaces";
        ArrayList<String> ts = new ArrayList<>();
        try {
            ResultSet rs = new UserDAO( connection ).executeStatement( sql );
            while( rs.next() ){
                ArrayList<String> sl = new ArrayList<String>();

                for(int i = 1 ; i <= rs.getMetaData().getColumnCount() ; i++){
                    if( rs.getString(i)==null )
                        sl.add( "(null)" );
                    else
                        sl.add( rs.getString(i) );
                }
                
                ts.add( sl.get(0) );
            }
            
        } catch (SQLException ex) {
            System.out.println("Error: "+ex);
            throw ex;
            //Logger.getLogger(UserBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ts;
    }
}
